package com.example.demo.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.model.AccessToken;
import com.example.demo.model.User;
import com.example.demo.service.AccessTokenService;
import com.example.demo.service.UserService;

@Controller
@RequestMapping("myacceccToken")
public class AccessTokenController {
	
	@Autowired
	private AccessTokenService accessTokenService;
	@Autowired
	private UserService userService;

	@GetMapping("checkToken")
	@ResponseBody
	public Object checkToken(String token) {
		System.out.println("正在请求myacceccToken : " + token);
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("code", 20000);
		try {
			AccessToken accessToken = accessTokenService.findByToken(token);
			if(accessToken==null) {
				throw new Exception("无效的token");
			}
			String username = accessToken.getUsername();
			User user = userService.findByUsername(username);
			result.put("message", user);
		} catch (Exception e) {
			result.put("code", 1);
			result.put("message", "请求错误"+e.getMessage());
		}
		return result;
	}
}
